#include "stdafx.h"
#include "ModuleVersionFileGroupListWindow.h"

CModuleVersionFileGroupListWindow::CModuleVersionFileGroupListWindow()
{
}

CModuleVersionFileGroupListWindow::~CModuleVersionFileGroupListWindow()
{
}

#define INSERT_COLUMN( iSubItem2, text, width, format)	\
	ZeroMemory( &column, sizeof( LVCOLUMN));\
	column.mask = LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH | LVCF_FMT;\
	column.fmt = format;\
	column.iSubItem = iSubItem2;\
	column.cx = width;\
	column.pszText = (char*)text;\
	column.cchTextMax = sizeof( text);\
	ListView_InsertColumn( m_hWnd, iSubItem2, &column);

bool CModuleVersionFileGroupListWindow::Create(HINSTANCE hInstance,HWND hParentWnd,UINT nChildID)
{
	RECT rcWindow;
	ZeroMemory( &rcWindow, sizeof( RECT));
	rcWindow.left = 0;
	rcWindow.right = 100;
	rcWindow.top = 0;
	rcWindow.bottom = 100;


	m_hWnd =::CreateWindowEx( WS_EX_CLIENTEDGE, "SysListView32", NULL, WS_VISIBLE | WS_CHILD | LVS_REPORT | LVS_SINGLESEL, rcWindow.left, rcWindow.top, RECT_WIDTH( rcWindow), RECT_HEIGHT( rcWindow), hParentWnd, (HMENU)nChildID, hInstance, NULL);
	if( m_hWnd == NULL)
		return false;

	ListView_SetExtendedListViewStyle( m_hWnd, LVS_EX_FULLROWSELECT | LVS_EX_FLATSB | LVS_EX_HEADERDRAGDROP);

	LVCOLUMN column;
	INSERT_COLUMN( 0, "Group Name", 150, LVCFMT_LEFT);
	
	m_GroupAll.index = 0xFF;
	strcpy_s( m_GroupAll.strGroupName, MAX_PATH, "All(*.*)");
	strcpy_s( m_GroupAll.strExt, MAX_PATH, "*");
	m_GroupAll.dwVersion = 0;

	m_GroupUnknown.index = 0;
	strcpy_s( m_GroupUnknown.strGroupName, MAX_PATH, "Unknown");
	strcpy_s( m_GroupUnknown.strExt, MAX_PATH, "*");
	m_GroupUnknown.dwVersion = 0;

	InsertGroup( &m_GroupAll);
	InsertGroup( &m_GroupUnknown);

	return true;
}

void CModuleVersionFileGroupListWindow::Release()
{
	m_ExtInfoMap.clear();
}

void CModuleVersionFileGroupListWindow::InsertGroup(CVersionCheck::sFileExtInfo* pInfo)
{
	if( m_ExtInfoMap.find( pInfo) != m_ExtInfoMap.end())
		return;

	LVITEM item;
	ZeroMemory( &item, sizeof( LVITEM));
	item.mask = LVIF_TEXT | LVIF_PARAM;
	item.pszText = (char*)pInfo->strGroupName;
	item.cchTextMax = sizeof( pInfo->strGroupName);
	item.lParam = (DWORD)pInfo;
	item.iItem = ListView_GetItemCount( m_hWnd);

	int iIndex = ListView_InsertItem( m_hWnd, &item);

	item.mask = LVIF_TEXT;
	item.iItem = iIndex;
	item.iSubItem = 0;
	item.pszText = (char*)pInfo->strGroupName;
	item.cchTextMax = sizeof( pInfo->strGroupName);
	ListView_SetItem( m_hWnd, &item);

	m_ExtInfoMap.insert( EXTINFO_MAP::value_type( pInfo, iIndex));	
}

void CModuleVersionFileGroupListWindow::Clear()
{
	m_ExtInfoMap.clear();

	ListView_DeleteAllItems( m_hWnd);

	InsertGroup( &m_GroupAll);
	InsertGroup( &m_GroupUnknown);
}